<template>
  <div class="evaluateAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div class="search-button" v-if="!contactAddShow && pageType !== 'detail'">
      <el-button
        v-if="pageType !== 'detail'"
        class="btn add"
        @click="openSelectUser()"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >添加用户</el-button
      >
      <el-button class="btn back" @click="handleClose()"
        ><el-icon class="icon" color="#fff"> <Back /> </el-icon
        >返回</el-button
      >
    </div>
    <el-collapse v-model="activeNames">
      <!-- 基础信息 -->
      <!-- <el-collapse-item
        name="1"
        v-if="!contactAddShow && pageType !== 'detail'"
        :class="contactAddShow ? 'customInfoUpClass' : 'customInfoDownClass'"
      >
        <template #title>
          <span class="circle"></span>
          <span style="font-weight: bold">角色信息</span>
        </template>
        <div class="evaluate-info">
          
        </div>
      </el-collapse-item> -->
      <el-collapse-item name="2" style="position: relative">
        <template #title>
          <span class="circle"></span>
          <span style="font-weight: bold">用户信息</span>
        </template>
        <el-table
          v-loading="loading"
          :data="userList"
          @selection-change="handleSelectionChange"
          border
          style="margin-top: 10px"
        >
          <el-table-column
            label="用户名称"
            prop="userName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="用户昵称"
            prop="nickName"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="邮箱"
            prop="email"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            label="手机"
            prop="phonenumber"
            :show-overflow-tooltip="true"
          />
          <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <dict-tag
                :options="sys_normal_disable"
                :value="scope.row.status"
              />
            </template>
          </el-table-column>
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template #default="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template #default="scope">
              <el-button
                link
                type="primary"
                icon="CircleClose"
                @click="cancelAuthUser(scope.row)"
                v-hasPermi="['system:role:remove']"
                >取消授权</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
        <select-user
          ref="selectRef"
          :roleId="queryParams.roleId"
          @ok="handleQuery"
        />
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import selectUser from "./selectUser";
import {
  allocatedUserList,
  authUserCancel,
  authUserCancelAll,
} from "@/api/system/role";
import { getAuthRole, updateAuthRole } from "@/api/system/user";
const emit = defineEmits(["refreshList", "update:evaluateAddShow"]);
const props = defineProps({
  pageType: {
    type: String,
    default: "add",
  },
});

const route = useRoute();
const { proxy } = getCurrentInstance();

const loading = ref(true);
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

const userList = ref([]);
const multiple = ref(true);
const total = ref(0);
const userIds = ref([]);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  roleId: route.params.roleId,
  userName: undefined,
  phonenumber: undefined,
});
const activeNames = ref(["1", "2"]);
const detailInfoShow = ref(false);
const contactAddShow = ref(false);
/** 查询授权用户列表 */
function getList() {
  loading.value = true;
  allocatedUserList(queryParams).then((response) => {
    userList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
// 返回按钮
function handleClose() {
  const obj = { path: "/system/role" };
  proxy.$tab.closeOpenPage(obj);
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
// 多选框选中数据
function handleSelectionChange(selection) {
  userIds.value = selection.map((item) => item.userId);
  multiple.value = !selection.length;
}
/** 打开授权用户表弹窗 */
function openSelectUser() {
  proxy.$refs["selectRef"].show();
}
/** 取消授权按钮操作 */
function cancelAuthUser(row) {
  proxy.$modal
    .confirm('确认要取消该用户"' + row.userName + '"角色吗？')
    .then(function () {
      return authUserCancel({ userId: row.userId, roleId: queryParams.roleId });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}
/** 批量取消授权按钮操作 */
function cancelAuthUserAll(row) {
  const roleId = queryParams.roleId;
  const uIds = userIds.value.join(",");
  proxy.$modal
    .confirm("是否取消选中用户授权数据项?")
    .then(function () {
      return authUserCancelAll({ roleId: roleId, userIds: uIds });
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("取消授权成功");
    })
    .catch(() => {});
}

getList();
</script>
<style lang="scss" scoped>
:deep(.el-collapse-item__header) {
  background-color: #f8faff;
}
.evaluateAdd {
  padding: 20px 30px;
  width: 100%;
  height: 100%;
  :deep(.el-date-editor.el-input, .el-date-editor.el-input__wrapper) {
    width: 268px !important;
    height: 34px !important;
  }
  /* .search-button {
    position: absolute;
    top: -45px;
    right: 60px;
    display: flex;
    .btn {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 90px;
      height: 34px;
      padding: 10px;
      // margin-right: 20px;
      font-size: 14px;
      border-radius: 4px;
      user-select: none;
      cursor: pointer;
    }
    .btn.save {
      color: #fff;
      background: rgba(67, 207, 124, 1);
      border: none;
      &:hover {
        background: rgba(67, 207, 124, 0.85);
      }
    }

    .btn.add {
      border: none;
      color: #fff;
      background: rgba(255, 159, 0, 1);
      &:hover {
        background: rgba(255, 159, 0, 0.85);
      }
    }

    .btn.back {
      border: none;
      color: #fff;
      background: rgba(153, 153, 153, 1);
      &:hover {
        background: rgba(153, 153, 153, 0.85);
      }
    }
    .btn.direction {
      border: none;
      color: #fff;
      background: rgba(153, 153, 153, 1);
      &:hover {
        background: rgba(153, 153, 153, 0.85);
      }
    }

    .icon {
      margin-right: 6px;
    }
  } */
  .circle {
    margin: 0 10px 0 20px;
    width: 6px;
    height: 6px;
    background: #007cef;
    border-radius: 50%;
  }
  .el-collapse,
  .el-collapse-item__wrap {
    border: none;
  }

  .search-input {
    width: 268px;
    height: 34px;
  }
  .evaluate-info {
    padding: 20px 10px 5px 10px;
    background-color: #f0f0f0;
    margin: 10px 0;
  }

  .button-position {
    top: 6px;
    right: 30px;
  }
  :deep(.el-input__inner) {
    height: 34px;
  }
  .customInfoUpClass {
    -webkit-animation: customInfoDown 0.8s ease-out;
  }
  .customInfoDownClass {
    -webkit-animation: customInfoDown 0.8s ease-out;
  }

  .contactInfoUpClass {
    -webkit-animation: contactInfoUp 0.8s ease-out;
  }
  .contactInfoDownClass {
    -webkit-animation: contactInfoUp 0.8s ease-out;
  }
  @-webkit-keyframes customInfoUp {
    0% {
      opacity: 0;
      -webkit-transform: translateY(80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
  @-webkit-keyframes customInfoDown {
    0% {
      opacity: 0;
      -webkit-transform: translateY(-80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
  @-webkit-keyframes contactInfoUp {
    0% {
      opacity: 0;
      -webkit-transform: translateY(80px);
    }

    100% {
      opacity: 1;
      -webkit-transform: translateY(0px);
    }
  }
  .pagination {
    margin: 20px auto;
  }
}
</style>
